const express = require('express');
const uuid = require('node-uuid');
const { Message,MessageList } = require('../utils/message');
const purchaseModel = require('../models/purchase');
const stockModel = require('../models/stock');
const materialInfoModel = require('../models/material');
const costModel = require('../models/cost');

const router = express.Router();

router.post('/',(req,res,next)=>{
  const {materialId,predictPrice,predictNum,remark,predictTotal}=req.body;
  const id = uuid.v1();
  const status='待分发';
  const realPrice='';
  const realNum='';
  const realTotal='';
  const purchaseDate='';
  const purchaseID='';
  const operator='';
  const purchase = new purchaseModel(
    {id,materialId,predictPrice,realPrice,predictNum,realNum,realTotal,remark,predictTotal,status,purchaseDate,purchaseID,operator}
    );
  purchase.save().then(data=>{
    if(data){
      res.json(Message(200,'添加成功'))
    }
  })
});

router.delete('/',(req,res,next)=>{
  const {id}=req.query;
  if(id){
    purchaseModel.deleteOne({id}).then(data=>{
      if(data.deletedCount!==0){
        res.json(Message(200,'删除成功！'))
      }else{
        res.json(Message(400,'删除失败'))
      }
    })
  }else{
    res.json(Message(400,'缺少参数id'))
  }
})

router.put('/',(req,res,next)=>{
  const {id}=req.query;
  const {materialId,name,status,operator,predictPrice,predictNum,realPrice,predictTotal,realNum,realTotal,purchaseDate}=req.body;
  const modifyObj={};
  modifyObj.status=status;
  if(status==='已分发') {
    if(typeof operator!=='undefined') modifyObj.operator=operator;
    if(typeof predictPrice!=='undefined') modifyObj.predictPrice=predictPrice;
    if(typeof predictNum!=='undefined') modifyObj.predictNum=predictNum;
    if(typeof predictTotal!=='undefined') modifyObj.predictTotal=predictTotal;
    if(typeof realPrice!=='undefined') modifyObj.realPrice=realPrice;
    if(typeof realNum!=='undefined') modifyObj.realNum=realNum;
    if(typeof realTotal!=='undefined') modifyObj.realTotal=realTotal;
    if(typeof purchaseDate!=='undefined') modifyObj.purchaseDate=purchaseDate;
  }else if(status==='送审'){
    if(realPrice) modifyObj.realPrice=realPrice;
    if(realNum) modifyObj.realNum=realNum;
    if(realTotal) modifyObj.realTotal=realTotal;
    if(purchaseDate) modifyObj.purchaseDate=purchaseDate;
  }
  if(id){
    purchaseModel.updateOne({id},modifyObj).then(data=>{
      if(data.acknowledged&&data.modifiedCount!==0){
        res.json(Message(200,'发送成功！'));
        if(status==='已完成'&&materialId){
          stockModel.findOne({id:materialId}).then(data=>{
            if(data){
              const newNum=data.number+Number(realNum);
              stockModel.updateOne({id:materialId},{number:newNum}).then(data=>{
                console.log('success!')
              })
            }else{
              materialInfoModel.findOne({id:materialId}).then(data=>{
                if(data){       
                  const stock=new stockModel({
                    id:data.id,
                    name: data.name,
                    introduction: data.introduction,
                    number: Number(realNum),
                    remark: ''
                  })
                  stock.save()
                }
              })
            }
          })
          const costs=new costModel({id,name,price:realPrice,number:realNum,cost:realTotal,costDate:purchaseDate,purpose: '采购'});
          costs.save().then(res=>{
            console.log('success!')
          })
        }
      }else{
        res.json(Message(400,'发送失败！'));
      }
    })
  }else{
    res.json(Message(400,'缺少参数id'))
  }
})

router.get('/',(req,res,next)=>{
  const {id,operator,status}=req.query;
  const queryObj={};
  if(id) queryObj.id=id;
  if(operator) queryObj.operator=operator;
  if(status) queryObj.status=status;
  purchaseModel.find(queryObj).count((err,count)=>{
    purchaseModel.find(queryObj).then(data=>{
      if(!id){
        res.json(MessageList(200,'',data,0,0,count));
      }else{
        res.json(Message(200,'',data));
      }
    })
  })
})

module.exports=router;